package com.anasit.motor.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

  // IWXAPI 是第三方app和微信通信的openapi接口
  private IWXAPI api;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 通过WXAPIFactory工厂，获取IWXAPI的实例
    api = WXAPIFactory.createWXAPI(this, WechatPlugin.APP_ID, false);
    api.registerApp(WechatPlugin.APP_ID);
    api.handleIntent(getIntent(), this);
  }

  @Override
  protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    setIntent(intent);
    api.handleIntent(intent, this);
  }

  // 微信发送请求到第三方应用时，会回调到该方法
  @Override
  public void onReq(BaseReq req) {
    // Toast.makeText(this, "openid = " + req.openId, Toast.LENGTH_SHORT).show();
  }

  // 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
  @Override
  public void onResp(BaseResp resp) {
    switch(resp.getType()){
      case ConstantsAPI.COMMAND_SENDAUTH://微信登录
        WechatPlugin.mCallBackContext.success(((SendAuth.Resp) resp).code);
        break;
      case ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX://微信分享
        break;
    }
    this.finish();
  }
}
